package com.example.chatrecycle.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ObservableField;
import androidx.lifecycle.MutableLiveData;
import androidx.recyclerview.widget.RecyclerView;

import com.example.chatrecycle.R;
import com.example.chatrecycle.databinding.ChatMessageItemBinding;
import com.example.chatrecycle.model.MsgEntity;

import java.util.List;

public class MyRecyclerViewAdapter extends RecyclerView.Adapter<MyRecyclerViewAdapter.MyViewHolder> {

    public class MyViewHolder extends  RecyclerView.ViewHolder{

        ChatMessageItemBinding chatMessageItemBinding ;


        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            //拿到item的布局对象
            chatMessageItemBinding = DataBindingUtil.bind(itemView);
        }
    }


    private ObservableField<List<MsgEntity>> msgs ;

    private MutableLiveData<List<MsgEntity>> msgs_m;


    //构造
    public MyRecyclerViewAdapter(ObservableField<List<MsgEntity>> msgs) {
        this.msgs = msgs;
    }

    public MyRecyclerViewAdapter(MutableLiveData<List<MsgEntity>> msgs_m) {
        this.msgs_m = msgs_m;
    }

    @NonNull
    @Override//把item的view视图给到viewHOlder 让其缓存起来
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        LayoutInflater from = LayoutInflater.from(parent.getContext());
        View inflate = from.inflate(R.layout.chat_message_item, parent,false);
        MyViewHolder myViewHolder = new MyViewHolder(inflate);
        return myViewHolder;
    }

    @Override//吧数据绑定给item
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
        MsgEntity msgEntity = msgs.get().get(position);
        //MsgEntity msgEntity = msgs_m.getValue().get(position) ;
        if (msgEntity.getState() == 0 ){
            //如果是别人，让下面的对话框隐藏
            holder.chatMessageItemBinding.otherLayout.setVisibility(View.VISIBLE);
            holder.chatMessageItemBinding.myLayout.setVisibility(View.GONE);
            holder.chatMessageItemBinding.otherMsg.setText(msgEntity.getName());

//            holder.upll.setVisibility(View.VISIBLE);
//            holder.downll.setVisibility(View.GONE);
//            holder.uptv.setText(msgEntity.getName());
        }else if (msgEntity.getState() == 1 ){
            holder.chatMessageItemBinding.myLayout.setVisibility(View.VISIBLE);
            holder.chatMessageItemBinding.otherLayout.setVisibility(View.GONE);
            holder.chatMessageItemBinding.myMsg.setText(msgEntity.getName());

//            holder.downll.setVisibility(View.VISIBLE);
//            holder.upll.setVisibility(View.GONE);
//            holder.downtv.setText(msgEntity.getName());
        }


    }

    @Override
    public int getItemCount() {
        return msgs.get().size();
    }

}
